#####
## September 2019
## Run this code file second in R
## This generates the tables/figures for main paper
#####

library(pscl)
library(foreign)
library(dplyr)
library(ggplot2)
library(xtable)

set.seed(43)

multiplot <- function(..., plotlist=NULL, file, cols=2, layout=NULL) {
  library(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

load(file="screening_190924.RData")


estimates_screeners<-cbind(as.data.frame(data_screeners[,1]),as.data.frame(data_screeners[,2]),as.data.frame(summary(model_screeners)[2]),as.data.frame(summary(model_screeners_nongrid)[2]),as.data.frame(summary(model_screeners_grid)[2]),as.data.frame(summary(model_screeners_mixed)[2]),as.data.frame(data_screeners[,692]))
colnames(estimates_screeners)<-c("respondent","responseid_wave1",  "attention", "attention_nongrid","attention_grid","attention_mixed","scr_pass")
## flip grid scale
estimates_screeners$attention_grid<-estimates_screeners$attention_grid* -1
estimates_screeners$attention_mixed<-estimates_screeners$attention_mixed* -1

estimates_screeners<-dplyr::select(estimates_screeners, -respondent)
data_merged_screeners<-cbind(data_screeners, estimates_screeners)
data_merged_screeners[data_merged_screeners==""]<-NA
cor(data_merged_screeners$attention, data_merged_screeners$attention_grid, use="complete.obs")
cor(data_merged_screeners$attention, data_merged_screeners$attention_nongrid, use="complete.obs")
cor(data_merged_screeners$attention, data_merged_screeners$attention_mixed, use="complete.obs")
### correlations with full Attentiveness Scale
xtable(as.data.frame(c(.70, .90, .91)))
cor(data_merged_screeners$scr_pass, data_merged_screeners$attention_grid, use="complete.obs")
cor(data_merged_screeners$scr_pass, data_merged_screeners$attention_nongrid, use="complete.obs")
cor(data_merged_screeners$scr_pass, data_merged_screeners$attention_mixed, use="complete.obs")
cors<-as.data.frame(cbind(c(.79, .86, .91),c(.70, .90, .91)))

xtable(cors)

cor(data_merged_screeners$attention_grid, data_merged_screeners$attention_nongrid, use="complete.obs")


cor(data_merged_screeners$attention, data_merged_screeners$attention_grid, use="complete.obs")
cor(data_merged_screeners$attention, data_merged_screeners$attention_nongrid, use="complete.obs")
cor(data_merged_screeners$attention, data_merged_screeners$attention_mixed, use="complete.obs")

library(ggplot2)
a<-ggplot(data_merged_screeners,aes(x=attention)) + geom_density(alpha=0.25, color="black")+geom_density(aes(x=attention_nongrid),alpha=0.25, color="red") + geom_density(aes(x=attention_grid),alpha=0.25, color="blue")+ geom_density(aes(x=attention_mixed),alpha=0.25, color="green")+
 theme_bw() 

#pdf(file="Figures/grid_nongrid_comparison.pdf")
a
#dev.off()

pass1<-mean(data_screeners$scr_web_w1, na.rm=T)
pass2<-mean(data_screeners$scr_problem_w1, na.rm=T)
pass3<-mean(data_screeners$scr_grid_ww2_w1, na.rm=T)
pass4<-mean(data_screeners$scr_grid_middle_w1, na.rm=T)
pass5<-mean(data_screeners$scr_grid_obama_w1, na.rm=T)
pass6<-mean(data_screeners$scr_grid_number_w1, na.rm=T)
pass7<-mean(data_screeners$scr_color_w1, na.rm=T)
pass8<-mean(data_screeners$scr_section_w1, na.rm=T)

pass<-as.data.frame(c(pass1, pass2,pass3,pass4,pass5,pass6,pass7,pass8) )
library(xtable)
xtable(pass)

parameters<-as.data.frame(model_screeners[6])
vote1_disc<-median(parameters$beta.Vote.1.Discrimination.D1)
vote2_disc<-median(parameters$beta.Vote.2.Discrimination.D1)
vote3_disc<-median(parameters$beta.Vote.3.Discrimination.D1)
vote4_disc<-median(parameters$beta.Vote.4.Discrimination.D1)
vote5_disc<-median(parameters$beta.Vote.5.Discrimination.D1)
vote6_disc<-median(parameters$beta.Vote.6.Discrimination.D1)
vote7_disc<-median(parameters$beta.Vote.7.Discrimination.D1)
vote8_disc<-median(parameters$beta.Vote.8.Discrimination.D1)
disc<-as.data.frame(c(vote1_disc, vote2_disc,vote3_disc,vote4_disc,vote5_disc,vote6_disc,vote7_disc,vote8_disc) )

vote1_diff<-median(parameters$beta.Vote.1.Difficulty)
vote2_diff<-median(parameters$beta.Vote.2.Difficulty)
vote3_diff<-median(parameters$beta.Vote.3.Difficulty)
vote4_diff<-median(parameters$beta.Vote.4.Difficulty)
vote5_diff<-median(parameters$beta.Vote.5.Difficulty)
vote6_diff<-median(parameters$beta.Vote.6.Difficulty)
vote7_diff<-median(parameters$beta.Vote.7.Difficulty)
vote8_diff<-median(parameters$beta.Vote.8.Difficulty)
diff<-as.data.frame(c(vote1_diff, vote2_diff,vote3_diff,vote4_diff,vote5_diff,vote6_diff,vote7_diff,vote8_diff) )

vote1_cut<-vote1_diff/vote1_disc
vote2_cut<-vote2_diff/vote2_disc
vote3_cut<-vote3_diff/vote3_disc
vote4_cut<-vote4_diff/vote4_disc
vote5_cut<-vote5_diff/vote5_disc
vote6_cut<-vote6_diff/vote6_disc
vote7_cut<-vote7_diff/vote7_disc
vote8_cut<-vote8_diff/vote8_disc
cut<-as.data.frame(c(vote1_cut, vote2_cut,vote3_cut,vote4_cut,vote5_cut,vote6_cut,vote7_cut,vote8_cut) )

iif<-matrix(data=NA, nrow=8, ncol=13)

attentions<-c(-1.5,-1.25,-1,-.75,-.5,-.25,0,.25,.5,.75,1,1.25,1.5)
for(i in 1:13){
	for(j in 1:8){
		iif[j,i]<-disc[j,]^2*pnorm((disc[j,]*attentions[i]-diff[j,]))*(1-pnorm((disc[j,]*attentions[i]-diff[j,])))
	}
}
iif2<-rowSums(iif, na.rm=T)
### full TIF
cbind(iif2)
## TIF for separeating shirkers and workers
#cbind(sort(iif[,5]))

items<-as.data.frame(c("scr_web_w1"," scr_problem_w1"," scr_grid_ww2_w1"," scr_grid_middle_w1"," scr_grid_obama_w1"," scr_grid_number_w1"," scr_color_w1"," scr_section_w1") )

type<-as.data.frame(c("non-grid"," non-grid"," grid"," grid"," grid"," grid"," non-grid","non-grid") )

#########
#### Table 1
##########
display<-cbind(type,items, pass,  diff,disc)
display<-as.data.frame(display)
colnames(display)<-c("type","items", "pass", "diff", "disc")
#xtable(display)
display<-display[c(1,2,7,8,3,4,5,6),]
#xtable(display)

## TIF
display<-cbind(display,round(iif2[c(1,2,7,8,3,4,5,6)], 2))

#IIF high attention, (attentiveness=1)
display<-cbind(display,round(iif[c(1,2,7,8,3,4,5,6),11], 2))

#IIF low attention, (attentiveness=-1)
display<-cbind(display,round(iif[c(1,2,7,8,3,4,5,6),3], 2))
xtable(display)


mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==0], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==1], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==2], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==3], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==4], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==5], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==6], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==7], na.rm=T)
mean(data_merged_screeners$attention[data_merged_screeners$scr_pass_w1==8], na.rm=T)


cor(data_merged_screeners$attention, data_merged_screeners$scr_pass_w1, use="complete.obs")
cor(data_merged_screeners$attention_grid, data_merged_screeners$scr_pass_w1, use="complete.obs")
cor(data_merged_screeners$attention_nongrid, data_merged_screeners$scr_pass_w1, use="complete.obs")
cor(data_merged_screeners$attention_mixed, data_merged_screeners$scr_pass_w1, use="complete.obs")

cor(data_merged_screeners$attention, data_merged_screeners$educ, use="complete.obs")
cor(data_merged_screeners$attention_grid, data_merged_screeners$educ, use="complete.obs")
cor(data_merged_screeners$attention_nongrid, data_merged_screeners$educ, use="complete.obs")

data_merged_screeners$kt_gain_wave1<-as.numeric(as.vector(data_merged_screeners$kt_gain_wave1))
data_merged_screeners$kt_loss_wave1<-as.numeric(as.vector(data_merged_screeners$kt_loss_wave1))

prop.table(table(data_merged_screeners$scr_pass_w1, data_merged_screeners$kt_gain_wave1), 1)
prop.table(table(data_merged_screeners$scr_pass_w1, data_merged_screeners$kt_loss_wave1), 1)


table(data_merged_screeners$kt_condition_wave1)

table(data_merged_screeners$kt_gain_wave1)
table(data_merged_screeners$kt_loss_wave1)

data_merged_screeners$kt_loss_wave1<-data_merged_screeners$kt_loss_wave1-1
data_merged_screeners$kt_gain_wave1<-data_merged_screeners$kt_gain_wave1-1

table(data_merged_screeners$kt_gain_wave1)
table(data_merged_screeners$kt_loss_wave1)

summary(lm(kt_loss_wave1~attention,data=data_merged_screeners))
summary(lm(kt_loss_wave1~attention_grid,data=data_merged_screeners))
summary(lm(kt_loss_wave1~attention_nongrid,data=data_merged_screeners))

summary(lm(kt_gain_wave1~attention,data=data_merged_screeners))
summary(lm(kt_gain_wave1~attention_grid,data=data_merged_screeners))
summary(lm(kt_gain_wave1~attention_nongrid,data=data_merged_screeners))

#### 
## Validation using The Tversky and Kahneman (1981) Framing Experiment
##
## KT treatment effect as function of attention
####
library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention,g=40)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention,attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
cors<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention, na.rm=T))
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))

data_merged_screeners$attention_groups2<- cut2(data_merged_screeners$attention,g=5)
data_merged_screeners3<-dplyr::select(data_merged_screeners, attention, attention_groups2 ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners3, attention_groups2)
cors2<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention, na.rm=T))
colnames(cors2)[2]<-"cors"
cors2$attention_groups3<-as.numeric(as.factor(cors2$attention_groups2))
kt_full_scale<-cors2
a<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+
  scale_x_continuous(limits = c(-2.25,1.8), breaks=c(-2,-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-.23,.6), labels=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6), breaks=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6))+ 
  xlab("Full Attentiveness Scale (8 Items)")+
  ylab("KT Treatment Effect")+
 theme_bw() 

#pdf(file="Figures/kt_fullscale.pdf")
a
#dev.off()


## KT treatment effect as function of attention
library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_nongrid,g=40)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_nongrid,attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
cors<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention_nongrid, na.rm=T))
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))

data_merged_screeners$attention_groups2<- cut2(data_merged_screeners$attention_nongrid,g=5)
data_merged_screeners3<-dplyr::select(data_merged_screeners, attention_nongrid, attention_groups2 ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners3, attention_groups2)
cors2<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention_nongrid, na.rm=T))
colnames(cors2)[2]<-"cors"
cors2$attention_groups3<-as.numeric(as.factor(cors2$attention_groups2))
kt_nongrid<-cors2

b<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+
  scale_x_continuous(limits = c(-2.25,1.8), breaks=c(-2,-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-.23,.6), labels=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6), breaks=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6))+ 
  xlab("Stand-alone Screener Scale (4 Items)")+
  ylab("KT Treatment Effect")+
 theme_bw() 

#pdf(file="Figures/kt_nongrid.pdf")
b
#dev.off()


## KT treatment effect as function of attention
library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_grid,g=40)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_grid,attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
cors<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention_grid, na.rm=T))
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))

data_merged_screeners$attention_groups2<- cut2(data_merged_screeners$attention_grid,g=5)
data_merged_screeners3<-dplyr::select(data_merged_screeners, attention_grid, attention_groups2 ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners3, attention_groups2)
cors2<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention_grid, na.rm=T))
colnames(cors2)[2]<-"cors"
cors2$attention_groups3<-as.numeric(as.factor(cors2$attention_groups2))
kt_grid<-cors2

c<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+
  scale_x_continuous(limits = c(-2.25,1.8), breaks=c(-2,-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-.23,.6), labels=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6), breaks=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6))+ 
   xlab("Grid Screener Scale (4 Items)")+
  ylab("KT Treatment Effect")+
 theme_bw() 

#pdf(file="Figures/kt_grid.pdf")
c
#dev.off()

library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_mixed,g=40)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_mixed,attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
cors<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention_mixed, na.rm=T))
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))

data_merged_screeners$attention_groups2<- cut2(data_merged_screeners$attention_mixed,g=5)
data_merged_screeners3<-dplyr::select(data_merged_screeners, attention_mixed, attention_groups2 ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
gp = dplyr::group_by(data_merged_screeners3, attention_groups2)
cors2<-dplyr::summarize(gp, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T), attention_mean=mean(attention_mixed, na.rm=T))
colnames(cors2)[2]<-"cors"
cors2$attention_groups3<-as.numeric(as.factor(cors2$attention_groups2))
kt_mixedscale<-cors2

d<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+
  scale_x_continuous(limits = c(-2.25,1.8), breaks=c(-2,-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-.23,.6), labels=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6), breaks=c(-.2,-.1,0, .1, .2,.3,.4,.5,.6))+ 
    xlab("Mixed Attentiveness Scale (4 Items)")+
  ylab("KT Treatment Effect")+
 theme_bw() 

#pdf(file="Figures/kt_mixed.pdf")
d
#dev.off()

#######
### Figure 1
######

pdf(file="Figures/Figure1_kt.pdf")
multiplot(a,c,b,d,cols=2)
dev.off()
save(kt_full_scale, kt_mixedscale, kt_grid, kt_nongrid, file="figure1_kt_tables.RData")

### compare with individual items
dplyr::summarize(data_merged_screeners, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))


#######
#######
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_ww2_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_ww2_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_middle_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_middle_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_obama_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_obama_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_number_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_grid_number_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_web_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_web_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_problem_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_problem_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_color_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_color_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_section_w1==1),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))
dplyr::summarize(data_merged_screeners[(data_merged_screeners$scr_section_w1==0),], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))



### compare above first quintile for each measurement strategy

data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
dplyr::summarize(data_merged_screeners2, mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))

data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
dplyr::summarize(data_merged_screeners2[as.numeric(data_merged_screeners2$attention_groups)==1,], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))


data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_mixed,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
dplyr::summarize(data_merged_screeners2[as.numeric(data_merged_screeners2$attention_groups)==1,], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))


data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_grid,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
dplyr::summarize(data_merged_screeners2[as.numeric(data_merged_screeners2$attention_groups)==1,], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))


data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_nongrid,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_jobs_1_wave1,  scale_services_1_wave1, kt_gain_wave1, kt_loss_wave1)
dplyr::summarize(data_merged_screeners2[as.numeric(data_merged_screeners2$attention_groups)==1,], mean(as.numeric(kt_loss_wave1), na.rm=T)-mean(as.numeric(kt_gain_wave1), na.rm=T))



######
#### Validation using ANES questions
#######

data_merged_screeners$scale_services_1_wave1<-as.numeric(as.vector(data_merged_screeners$scale_services_1_wave1))
data_merged_screeners$scale_jobs_1_wave1<-as.numeric(as.vector(data_merged_screeners$scale_jobs_1_wave1))
data_merged_screeners$scale_incomediffs_1_wave1<-as.numeric(as.vector(data_merged_screeners$scale_incomediffs_1_wave1))

data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==1 & data_merged_screeners$ideo2_wave1==1]<-1
data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==1 & data_merged_screeners$ideo2_wave1==2]<-2
data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==3 & data_merged_screeners$ideo4_wave1==1]<-3
data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==3 & data_merged_screeners$ideo4_wave1==2]<-4
data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==3 & data_merged_screeners$ideo4_wave1==3]<-5
data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==2 & data_merged_screeners$ideo3_wave1==2]<-6
data_merged_screeners$ideo7[data_merged_screeners$ideo1_wave1==2 & data_merged_screeners$ideo3_wave1==1]<-7
table(data_merged_screeners$ideo7)

#install.packages(“car”)
library(car)

data_merged_screeners$scale_services_1_wave1b <-car::recode(data_merged_screeners$scale_services_1_wave1, '1=7; 2=6; 3=5; 5=3; 6=2; 7=1')
data_merged_screeners$q123_7_wave1b <-car::recode(data_merged_screeners$q123_7_wave1, '1=7; 2=6; 3=5; 5=3; 6=2; 7=1')


####
## Relation between various measures of attention and correlation between income and 
## services ANES scales (flipped scales, so attention should have strong relationship with ## them)
####

####
## Difference in flipped and non-flipped scales
###
## Correlation between items as function of attention
library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention,g=40)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)


cors1<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors2<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors<-cors1-cors2
cors$attention_groups<-cors1$attention_groups
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention, na.rm=T))
cors<-merge(cors, cors.means, by="attention_groups")

library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)
cors21<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors22<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors2<-cors21-cors22
cors2$attention_groups<-cors21$attention_groups
colnames(cors2)[2]<-"cors"
cors2$attention_groups2<-as.numeric(as.factor(cors2$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention, na.rm=T))
cors2<-merge(cors2, cors.means, by="attention_groups")
anes_full_scale<-cors2[order(cors2$attention_mean),]
a<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+	
    scale_x_continuous(limits = c(-1.8,1.75), breaks=c(-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-1.73,.6), breaks=c(-1.5,-1,-.5,0,.5))+
  xlab("Full Attentiveness Scale (8 Items)")+
  ylab("Diff in Corr. between Reverse-coded Scales 
  and Same-coded Scales")+ theme_bw() 



## Correlation between items as function of attention
library (Hmisc)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_grid ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
data_merged_screeners2<-subset(data_merged_screeners2,!is.na(scale_jobs_1_wave1))
data_merged_screeners2$attention_groups<- cut2(data_merged_screeners2$attention_grid,g=40)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners2, attention_groups)

cors1<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors2<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors<-cors1-cors2
cors$attention_groups<-cors1$attention_groups
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention_grid, na.rm=T))
cors<-merge(cors, cors.means, by="attention_groups")

library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_grid,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)
cors21<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors22<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors2<-cors21-cors22
cors2$attention_groups<-cors21$attention_groups
colnames(cors2)[2]<-"cors"
cors2$attention_groups2<-as.numeric(as.factor(cors2$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention_grid, na.rm=T))
cors2<-merge(cors2, cors.means, by="attention_groups")
anes_grid<-cors2[order(cors2$attention_mean),]
b<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+	
    scale_x_continuous(limits = c(-1.8,1.75), breaks=c(-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-1.73,.6), breaks=c(-1.5,-1,-.5,0,.5))+
  xlab("Grid Screener Scale (4 Items)")+
  ylab("Diff in Corr. between Reverse-coded Scales 
  and Same-coded Scales")+ theme_bw() 



## Correlation between items as function of attention
library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_nongrid,g=40)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)

cors1<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors2<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors<-cors1-cors2
cors$attention_groups<-cors1$attention_groups
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention_nongrid, na.rm=T))
cors<-merge(cors, cors.means, by="attention_groups")

library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_nongrid,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)
cors21<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors22<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors2<-cors21-cors22
cors2$attention_groups<-cors21$attention_groups
colnames(cors2)[2]<-"cors"
cors2$attention_groups2<-as.numeric(as.factor(cors2$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention_nongrid, na.rm=T))
cors2<-merge(cors2, cors.means, by="attention_groups")
anes_nongrid<-cors2[order(cors2$attention_mean),]
c<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+	
    scale_x_continuous(limits = c(-1.8,1.75), breaks=c(-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-1.73,.6), breaks=c(-1.5,-1,-.5,0,.5))+
    xlab("Stand-alone Screener Scale (4 items)")+
  ylab("Diff in Corr. between Reverse-coded Scales 
  and Same-coded Scales")+   theme_bw() 

library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_mixed,g=35)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)


cors1<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors2<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors<-cors1-cors2
cors$attention_groups<-cors1$attention_groups
colnames(cors)[2]<-"cors"
cors$attention_groups2<-as.numeric(as.factor(cors$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention_mixed, na.rm=T))
cors<-merge(cors, cors.means, by="attention_groups")

library (Hmisc)
data_merged_screeners$attention_groups<- cut2(data_merged_screeners$attention_mixed,g=5)
data_merged_screeners2<-dplyr::select(data_merged_screeners,  attention_groups ,scale_incomediffs_1_wave1,  scale_services_1_wave1,scale_jobs_1_wave1)
gp = dplyr::group_by(data_merged_screeners2, attention_groups)
gp2 = dplyr::group_by(data_merged_screeners, attention_groups)
cors21<-dplyr::summarize(gp, cor(scale_jobs_1_wave1, scale_services_1_wave1, use="complete.obs"))
cors22<-dplyr::summarize(gp, cor(scale_incomediffs_1_wave1, scale_jobs_1_wave1, use="complete.obs"))
cors2<-cors21-cors22
cors2$attention_groups<-cors21$attention_groups
colnames(cors2)[2]<-"cors"
cors2$attention_groups2<-as.numeric(as.factor(cors2$attention_groups))
cors.means<-dplyr::summarize(gp2, attention_mean=mean(attention_mixed, na.rm=T))
cors2<-merge(cors2, cors.means, by="attention_groups")
anes_mixedscale<-cors2[order(cors2$attention_mean),]
d<-
	ggplot(aes(x=attention_mean, y=cors), data= cors) + 	
  #geom_point() +  
  geom_point( data=cors2) +  
  stat_smooth(method="loess")+	
    scale_x_continuous(limits = c(-1.8,1.75), breaks=c(-1.5,-1,-.5,0,.5,1,1.5))+
  scale_y_continuous(limits = c(-1.73,.6), breaks=c(-1.5,-1,-.5,0,.5))+
    xlab("Mixed Attentiveness Scale (4 items)")+
  ylab("Diff in Corr. between Reverse-coded Scales 
  and Same-coded Scales")+
   theme_bw() 
   
  ### Diff in corr between reverse-coded scales with same-coded scales

#######
### Figure 2
######
pdf(file="Figures/Figure2_DiD_jobs_income_income_services.pdf")
multiplot(a,b,c,d,cols=2)
dev.off()

save(anes_full_scale, anes_mixedscale, anes_grid, anes_nongrid, file="figure2_anes_tables.RData")

scaled_estimates_screeners<-dplyr::select( data_merged_screeners,
respondent,
responseid_wave1,
attention,
attention_nongrid,
attention_grid,
attention_mixed,
scr_pass)

scaled_estimates_screeners<-dplyr::rename(scaled_estimates_screeners,attention_fullscale=attention)

write.dta(scaled_estimates_screeners,file="scaled_estimates_screeners.dta")